﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;

public partial class Modules_UserManager_ucAddUserGroup : System.Web.UI.UserControl
{
    UserManager _userGroupManager = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserManager_UserGroupEdit"] == "Edit")
            {
                btnSave.Text = "Update";
                LoadData();
            }
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchUserGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["UserManager_UserGroupEdit"].ToString() == "Edit")
            Edit();
        else
            Add();
        //Clear();
    }

    void LoadData()
    {
        string condition = "US_GroupID = " + Session["UserManager_UserGroupID"] + " ";
        DataSet dsUser = _userGroupManager.GetData("UserGroup", condition);
        string dateofCreation = dsUser.Tables[0].Rows[0]["US_CreatedDate"].ToString();

        txtGroupName.Text = dsUser.Tables[0].Rows[0]["US_GroupName"].ToString();
        txtDescription.Text = dsUser.Tables[0].Rows[0]["US_Description"].ToString();
        txtCreatedBy.Text = dsUser.Tables[0].Rows[0]["US_CreatedBy"].ToString();
        if (dsUser.Tables[0].Rows[0]["US_CreatedDate"] != DBNull.Value)
            txtDate.Text = dateofCreation.Substring(0, dateofCreation.IndexOf(" "));

    }

    void Add()
    {
        try
        {

            UserDetails userGroup = new UserDetails();

            userGroup.GroupName = txtGroupName.Text;
            userGroup.GroupDescription = txtDescription.Text;
            userGroup.CreatedBy = txtCreatedBy.Text;
            userGroup.DateOfCreation = txtDate.Text;

            if (!_userGroupManager.AddUserGroup(userGroup))
            {
                Response.Write("<script>alert('Utility Group Already Exist')</script>");
            }
            else
                Response.Write("<script>alert('Added Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Utility Group Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }


    void Edit()
    {
        try
        {
            UserDetails userGroupEdit = new UserDetails();
            userGroupEdit.GroupID = Session["UserManager_UserGroupID"].ToString();
            userGroupEdit.GroupName = txtGroupName.Text;
            userGroupEdit.GroupDescription = txtDescription.Text;
            userGroupEdit.CreatedBy = txtCreatedBy.Text;
            userGroupEdit.DateOfCreation = txtDate.Text;

            if (_userGroupManager.EditUserGroup(userGroupEdit))
                Response.Write("<script>alert('Modified Successfully!')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "User Group Modification Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    protected void imbDate_Click(object sender, ImageClickEventArgs e)
    {
       // calDate.Visible = true;
    }
    protected void calDate_SelectionChanged(object sender, EventArgs e)
    {
       // txtDate.Text = calDate.SelectedDate.ToShortDateString();
       // calDate.Visible = false;
    }
    
}
